﻿using System;
using System.Linq;
using System.Reflection;
using Weapp.Data.Entities;

namespace Weapp.Data
{
    public class WeappRepository : IWeappRepository
    {
        #region PrivateMembers
        private WeappContext _ctx;
        #endregion


        public WeappRepository(WeappContext ctx)
        {
            _ctx = ctx;
        }
        public bool SaveAll()
        {
            return _ctx.SaveChanges() > 0;
        }

        #region SizeCategories
        public IQueryable<SizeCategory> GetAllSizeCategories()
        {
            return _ctx.SizeCategories
                .AsQueryable();
        }
        public SizeCategory GetSizeCategory(int sizeCategoryId)
        {
            return _ctx.SizeCategories
                .SingleOrDefault(c => c.SizeCategoryId == sizeCategoryId);
        }
        public bool Insert(SizeCategory sizeCategory)
        {
            try
            {
                _ctx.SizeCategories
                    .Add(sizeCategory); ;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(SizeCategory originalSizeCategory, SizeCategory sizeCategory)
        {
            try
            {
                copy(sizeCategory, originalSizeCategory);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        public bool Delete(int sizeCategoryId)
        {
            try
            {
                var entity = _ctx.SizeCategories.Find(sizeCategoryId);
                if (entity != null)
                {
                    _ctx.SizeCategories.Remove(entity);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        #endregion

        #region Characters
        public IQueryable<Character> GetAllCharacters()
        {
            return _ctx.Characters
                .AsQueryable();
        }
        public Character GetCharacter(int characterId)
        {
            return _ctx.Characters
                .Where(c => c.CharacterId == characterId)
                .SingleOrDefault();
        }
        public bool Insert(Character character)
        {
            try
            {
                _ctx.Characters
                    .Add(character);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        private void copy<T>(T source, T target)
        {
            foreach (PropertyInfo propertyInfo in source.GetType().GetProperties())
            {
                propertyInfo.SetValue(target, propertyInfo.GetValue(source), null);
            }
        }


        public bool Insert(UserDnD userDnD)
        {
            try
            {
                _ctx.Usuarios.Add(userDnD);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
    }
}
